﻿using System;
using System.ComponentModel;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Win.Templates.ActionContainers;
using DevExpress.XtraBars;
using DevExpress.ExpressApp;
using Hdt.TransOcean.Dal;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using DevExpress.XtraEditors.Controls;

namespace Hdt.TransOcean.Presentation
{
    public partial class FormAirline : DevExpress.ExpressApp.Win.Templates.XtraFormTemplateBase, ISupportClassicToRibbonTransform
    {
        private EOperation _operation;
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CheckTransformToRibbon();
        }
        protected override IModelFormState GetFormStateNode()
        {
            if (View != null)
            {
                return TemplatesHelper.GetFormStateNode(View.Id);
            }
            else
            {
                return base.GetFormStateNode();
            }
        }
        public override void SetSettings(IModelTemplate modelTemplate)
        {
            base.SetSettings(modelTemplate);
            formStateModelSynchronizerComponent.Model = GetFormStateNode();
        }
        [Obsolete("Use the DetailViewForm() constructor"), Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public FormAirline(XafApplication application) : this() { }
        public FormAirline()
        {
            InitializeComponent();
        }

        public FormAirline(EOperation operation, string code = null)
        {
            InitializeComponent();

            this._operation = operation;

            Presentation.BindGridLookupEdit(gridLookUpEditAirlineType, Presentation.GetData(EObject.AirlineType));
            Presentation.DisplayButtonByOperation(hdtManageControl1,operation);
            if (this._operation == EOperation.New)
            {
                txtCode.Enabled = true;
                gridLookUpEditAirlineType.EditValue = 1;
            }
                //txtCode.Text = Dao.GetAutoCode("Customer");
            if (operation == EOperation.Edit)
            {
                txtCode.Enabled = false;
                if (!String.IsNullOrEmpty(code))
                {
                    Airline airline = Dao.SelectAirline(code)[0];

                    txtCode.Text = airline.code;
                    txtName.Text = airline.name;
                    txtDescription.Text = airline.description;
                    gridLookUpEditAirlineType.EditValue = airline.type;
                    chkDeleted.EditValue = airline.deleted;
                }
            }
        }

        private void hdtManageControl1_HdtNewButtonClick()
        {
            ResetAirlineInfo();
            Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.New);
        }

        private void ResetAirlineInfo()
        {
            txtCode.Text = String.Empty;
            txtName.Text = String.Empty;
            txtDescription.Text = String.Empty;
            gridLookUpEditAirlineType.EditValue = 1;
            chkDeleted.EditValue = 0;
        }

        private void hdtManageControl1_HdtSaveButtonClick()
        {
            string message = "Thêm thành công!";
            //bool flag = true;
            Dao dao = new Dao();
            Airline airline = new Airline();
            Presentation.GetAirline(ref airline,
                this.txtCode.Text.TrimEnd(),
                this.txtName.Text.TrimEnd(),
                this.txtDescription.Text.TrimEnd());
            try
            {
                if (Presentation.ValidAirline(airline) == true)
                {
                    if (this._operation == EOperation.Edit)
                    {
                        dao.UpdateAirline(airline);
                    }
                    else if (this._operation == EOperation.New)
                    {
                        Dao.InsertAirLine(airline);
                        ResetAirlineInfo();
                    }
                    Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.Save);
                }
            }
            catch (Exception e)
            {
                if (e != null)
                {
                //    flag = false;
                    message = e.ToString();
                }
            }
            finally {
                MessageBox.Show(message, "Hãng hàng không - Lưu");
            }
        }


        private void hdtManageControl1_HdtEditButtonClick()
        {
            Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.Edit);
        }

        private void hdtManageControl1_HdtDeleteButtonClick()
        {
            if (!String.IsNullOrEmpty(txtCode.Text))
            {
                try
                {
                    if (MessageBox.Show("Bạn muốn xóa không?", "Hãng hàng không", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
                    {
                        if (Dao.DeleteAirlineByCode(txtCode.Text) == true)
                        {
                            MessageBox.Show("Xóa thành công", "Hãng hàng không");
                            Presentation.DisplayButtonByOperation(hdtManageControl1, EOperation.Delete);
                        }
                    }
                }
                catch (Exception e)
                {

                    MessageBox.Show(e.ToString());
                }
            }
        }

        private void hdtManageControl1_HdtCloseButtonClick()
        {
            this.Close();
        }

        private void hdtManageControl1_HdtImportButtonClick()
        {
            try
            {
                Dao.ImportExcel(EObject.Airline);
            }
            catch (Exception e){
                MessageBox.Show(e.ToString());
            }
        }

        private void hdtManageControl1_HdtExportButtonClick()
        {
            List<string[]> rows = new List<string[]>();
            rows.Add(new string[] { "code1", "name", "description" });
            rows.Add(new string[] { "code2", "name", "description" });
            rows.Add(new string[] { "code3", "name", "description" });
            rows.Add(new string[] { "code4", "name", "description" });

            try
            {
               Dao.ExportExcel(EObject.Airline,"airline.xls", rows);
            }catch(Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void FormAirline_Load(object sender, EventArgs e)
        {
            
        }


        
    }
    
}
